package com.yh.controller.admin;

import com.yh.config.OtherSource;
import com.yh.service.DirectorService;
import com.yh.utils.Constants;
import com.yh.utils.ImgUrl;
import com.yh.utils.UploadAndDownload;
import com.yh.vo.DirectorVO;
import com.yh.vo.ResponseVO;
import io.minio.MinioClient;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@RestController
@RequestMapping("/admin/director")
public class DirectorController {
    @DubboReference(check = false)
    private DirectorService directorService;
    @Autowired
    private OtherSource otherSource;
    @Autowired
    private MinioClient minioClient;

    /**
     * 查询所有的导演信息
     * 服务地址 http://localhost:8081/admin/director/all
     */
    @RequestMapping("all")
    public ResponseVO findDirectorAll() {
        List<DirectorVO> directorVos = directorService.findDirectorAll();
        return !ObjectUtils.isEmpty(directorVos) ? ResponseVO.buildSuccessInstance(directorVos) :
                ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
    }

    /**
     * 添加影片类型
     * 服务地址 http://localhost:8081/admin/director/add
     */
    @RequestMapping("add")
    public ResponseVO addDirector(DirectorVO vo) {
        MultipartFile file = vo.getDirectorFile();
        // 如果选中了图片就就执行图片的上传和头像信息的修改
        if (!ObjectUtils.isEmpty(file) && file.getSize() > 0) {
            // 海报上传
            UploadAndDownload.uploadToMinIo(minioClient, otherSource.directorFileUrl, file);
            // 将海报上传到本次磁盘（Win环境开发测试）
            UploadAndDownload.uploadToDisk(otherSource.uploadPath, file);
            // 将海报上传到Nginx本地（Linux环境下的备用路径。MinIO服务器无法使用时的备用选择）
            UploadAndDownload.uploadToNginxLocalhost(otherSource.nginxPath, file, otherSource.directorFileUrl);
            // 补全影片海报的基本地址
            vo.setDirectorPicture(ImgUrl.spliceBasicUrl(otherSource.directorFileUrl, file.getOriginalFilename()));
        }
        directorService.addDirector(vo);
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS);
    }
}
